package demo.jdbc.dao;

import java.io.*;
import java.sql.SQLException;
import java.util.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.jdbc.model.Bookmark;

public class JdbcMain {
	public static void main(String[] params) throws SQLException, IOException {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		FunnyBeanClass fb = (FunnyBeanClass) ctx.getBean("funnyBean");
		System.out.println("fb = " + fb);
		Bookmark b=null;
		byte buf[] = new byte[64];
		List<Bookmark> bms = new ArrayList<Bookmark>();
		BookmarkDAO dao = (BookmarkDAO) ctx.getBean("bookmarkDAO");
		if ( params.length ==0)
			{
			 
			 b = new Bookmark("http://www.aldaily.com", "Arts and Letters Daily","/home/student/workspace/imageTest/up4kman.gif");
			}
		else
		{
			System.err.println("wew");
		    b = new Bookmark("http://www.aldaily.com", "Arts and Letters Daily",params[0]);
		}
		dao.save(b);
		bms = dao.findAll();
		FileInputStream fs = null;
		FileOutputStream os = null;
		
        for (int i=0;i<bms.size();i++)
        {
        	b=bms.get(i);
        	fs = b.getAttachment();
        	File file = new File("file"+i+".gif");
        	os = new FileOutputStream(file);
        	if (! (fs == null))
        	{
        	while (fs.available()>0)
        	{	
        	  fs.read(buf);
        	  os.write(buf);
        	}
        	}
        	if (! (fs == null))
        	{
            fs.close();
        	os.close();
        	}
        	
        	//File fin = new File()
        }

	}
}
